In [1]:
# This script calls the module 'modAdv.py'  which contains the function 
# 'primes_adv()' to find all of the prime numbers between 'a'
# and 'b'.  It's an "advanced" version of the function 'primes()'.  There's
# nothing different about how we're calling the function.  It's just that
# what's executed within the actual function is slightly more sophisticated
# than what's in 'primes()'.

# 'primes_adv()' maintains a file of the sequential prime numbers that it
# has previously found.  Then, if a user asks for prime numbers that it has
# previously found, it just consults the list and tells the user the
# numbers they want to know without doing any searching.  Only if the user
# has asked for prime numbers that it has never found before, does it
# actually implement a search.

# In addition, 'primes_adv()' uses this list of prime numbers to
# intelligently search for new prime numbers. To check if n is a prime
# number, it is only necessary to check if it is a factor of any of the prime
# numbers less than n.  For example, we don't need to check if 27 is a
# factor of n.  If it is, then 3 is also a factor of n and we would have
# already determined that n is not prime.  By eliminating as many factors
# from our search as possible, we can greatly enhance the efficiency of our
# search for new prime numbers.

# As of Sept. 28, 2020, all of the prime number between 1 and about 1.317e9 
# have been found and sequentially listed by 'primes_adv()'.  So far, the 
# function has sequentially listed 66,051,364 prime numbers.

# Note that this is no where close to the largest prime number ever found.
# As I type this, the largest know prime number is 2^(57,885,161) - 1.  A
# number that is a ridiculous 17,425,170 digits long.  However, people do
# not know the sequential list of primes up to this number.

# Note that, after creating this script, I read that people use much faster
# sieve programs to find sequential prime numbers.

# The function that this script calls is named 'primes_adv(a, b)'. It finds all
# of the prime numbers between input integers a and b.  The code saved in
# 'modAdv.py' is shown below without any explanations.  If you open the
# file 'modAdv.py', you will find some additional comments that help
# explain the purpose of the various lines of code.

# To run this function, you will need to have the files 'modAdv.py',
# 'primes_max.txt', and 'primes_list.txt' in the same directory as the
# script that calls 'primes_adv()'.  'primes_list.txt' is inside
# 'primes_list.zip' which can be downloaded from my website.  With a list
# all prime numbers less than 1.317e9.  When unzipped, 'primes_list.txt' is 
# about 650 Mb.

#def primes_adv(a, b):
#     piN = []
#     if a % 1 == 0 and b % 1 == 0 and a > 0 and b > 0 and b > a:
#         import numpy as np 
#         import pandas as pd
#         # with open('primes_list.txt', 'r') as f:
#         #     lineList = f.readlines()
#         # with open("primes_list.txt", "ab") as f:
#         #        if len(lineList[-1].split('\n')) == 1:
#         #             f.write(b"\n")
#         primeMax = int(np.loadtxt("primes_max.txt"))
#         primeNums = pd.read_csv("./primes_list.txt", delimiter="\n", header = None).values[:, 0]
#         if b <= primeMax:
#             i = 0
#             while i <= len(primeNums) - 1 and primeNums[i] <= b:
#                 if primeNums[i] >= a:
#                     piN = piN + [primeNums[i]]
#                 i += 1
#             f = np.array(piN) 
#             n = len(f) 
#         elif b > primeMax:
#              for i in range(primeMax + 1, int(b) + 1):
#                  cnt = 0
#                  if i != 1 and i % 2 == 1 and sum(map(int, str(i))) % 3 != 0\
#                  and i % 10 != 5 or i == 2 or i == 3 or i == 5:
#                      j = 3
#                      while primeNums[j] < i/primeNums[j - 1] and cnt == 0:
#                          if i % primeNums[j] == 0: 
#                              cnt = 1
#                              primeMax = i
#                          j += 1
#                      if cnt == 0:
#                          primeMax = i
#                          primeNums = np.append(primeNums, i)
#                          np.savetxt('primes_max.txt', np.array([primeMax]), fmt='%i', delimiter='\n')
#                          with open("primes_list.txt", "ab") as f:
#                              np.savetxt(f, np.array([i]), fmt='%i')
#                  else:
#                      primeMax = i
#                      # np.savetxt('primes_max.txt', np.array([primeMax]), fmt='%i', delimiter='\n')
#                      # We could update primes_max.txt, but I commented out the line above to save time (i.e. not necessary to constantly update primes_max.txt.
#              np.savetxt('primes_max.txt', np.array([primeMax]), fmt='%i', delimiter='\n')
#              i = 0
#              while i <= len(primeNums) - 1 and primeNums[i] <= b:
#                  if primeNums[i] >= a:
#                      piN = piN + [primeNums[i]]
#                  i += 1
#              f = np.array(piN) 
#              n = len(f) 
#     else:
#         f = 'ERROR: a and b in primes(a, b) must be positive integers with b > a.'
#         n = -1 
#     return f, n                 
In [4]:
from datetime import datetime
print(datetime.now())
n_min = 20e6
n_max = 20.01e6;
import modAdv
xx, x = modAdv.primes_adv(n_min, n_max)
print(datetime.now())
print('A list of the', x, 'sequential prime numbers between', int(n_min),\
      'and', int(n_max))
xx
2021-03-11 09:10:40.475231
2021-03-11 09:10:49.997399
A list of the 594 sequential prime numbers between 20000000 and 20010000
Out[4]:
array([20000003, 20000023, 20000033, 20000047, 20000059, 20000063,
       20000069, 20000077, 20000081, 20000093, 20000107, 20000147,
       20000153, 20000159, 20000161, 20000171, 20000213, 20000221,
       20000243, 20000269, 20000287, 20000297, 20000303, 20000311,
       20000327, 20000329, 20000339, 20000353, 20000359, 20000377,
       20000389, 20000429, 20000443, 20000471, 20000503, 20000507,
       20000531, 20000537, 20000543, 20000567, 20000569, 20000573,
       20000599, 20000621, 20000623, 20000641, 20000671, 20000689,
       20000693, 20000707, 20000713, 20000723, 20000753, 20000779,
       20000791, 20000801, 20000821, 20000837, 20000839, 20000843,
       20000861, 20000867, 20000873, 20000879, 20000909, 20000917,
       20000933, 20000951, 20000969, 20000971, 20001001, 20001019,
       20001029, 20001031, 20001067, 20001073, 20001083, 20001139,
       20001151, 20001161, 20001181, 20001203, 20001217, 20001227,
       20001239, 20001253, 20001259, 20001263, 20001269, 20001277,
       20001301, 20001307, 20001341, 20001361, 20001389, 20001439,
       20001451, 20001491, 20001517, 20001521, 20001551, 20001557,
       20001613, 20001659, 20001679, 20001703, 20001763, 20001769,
       20001797, 20001799, 20001811, 20001823, 20001833, 20001841,
       20001847, 20001853, 20001899, 20001901, 20001929, 20001937,
       20001959, 20001977, 20001983, 20002007, 20002027, 20002061,
       20002067, 20002097, 20002123, 20002133, 20002159, 20002187,
       20002189, 20002201, 20002261, 20002313, 20002321, 20002331,
       20002337, 20002361, 20002363, 20002369, 20002397, 20002399,
       20002417, 20002429, 20002453, 20002457, 20002487, 20002529,
       20002531, 20002537, 20002553, 20002579, 20002603, 20002613,
       20002639, 20002651, 20002667, 20002669, 20002673, 20002679,
       20002691, 20002729, 20002733, 20002739, 20002757, 20002771,
       20002799, 20002823, 20002831, 20002841, 20002907, 20002919,
       20002933, 20002951, 20002979, 20002981, 20003003, 20003023,
       20003047, 20003057, 20003063, 20003089, 20003107, 20003117,
       20003131, 20003149, 20003161, 20003167, 20003171, 20003173,
       20003177, 20003201, 20003227, 20003231, 20003267, 20003287,
       20003293, 20003297, 20003317, 20003369, 20003383, 20003393,
       20003443, 20003461, 20003497, 20003513, 20003521, 20003549,
       20003563, 20003567, 20003597, 20003609, 20003663, 20003671,
       20003677, 20003681, 20003693, 20003699, 20003701, 20003719,
       20003801, 20003803, 20003821, 20003833, 20003857, 20003861,
       20003869, 20003887, 20003899, 20003909, 20003923, 20003927,
       20003933, 20003957, 20003969, 20003981, 20004001, 20004011,
       20004031, 20004041, 20004059, 20004071, 20004107, 20004133,
       20004137, 20004139, 20004191, 20004199, 20004211, 20004217,
       20004221, 20004233, 20004239, 20004253, 20004289, 20004307,
       20004311, 20004323, 20004343, 20004367, 20004419, 20004431,
       20004451, 20004487, 20004497, 20004521, 20004527, 20004533,
       20004541, 20004559, 20004581, 20004583, 20004667, 20004683,
       20004689, 20004709, 20004713, 20004727, 20004731, 20004749,
       20004773, 20004811, 20004821, 20004823, 20004833, 20004851,
       20004883, 20004893, 20004923, 20004931, 20004947, 20004973,
       20004977, 20004979, 20004997, 20005003, 20005021, 20005087,
       20005093, 20005129, 20005159, 20005163, 20005177, 20005207,
       20005213, 20005231, 20005259, 20005289, 20005291, 20005301,
       20005313, 20005319, 20005343, 20005351, 20005357, 20005361,
       20005367, 20005369, 20005393, 20005397, 20005429, 20005439,
       20005441, 20005451, 20005459, 20005483, 20005501, 20005507,
       20005511, 20005519, 20005523, 20005529, 20005541, 20005589,
       20005603, 20005637, 20005649, 20005673, 20005681, 20005693,
       20005709, 20005717, 20005721, 20005729, 20005763, 20005807,
       20005823, 20005829, 20005847, 20005849, 20005861, 20005871,
       20005873, 20005877, 20005891, 20005903, 20005907, 20005919,
       20005949, 20005981, 20006003, 20006023, 20006047, 20006053,
       20006057, 20006083, 20006087, 20006101, 20006123, 20006149,
       20006177, 20006179, 20006209, 20006219, 20006227, 20006243,
       20006279, 20006339, 20006351, 20006359, 20006419, 20006423,
       20006429, 20006431, 20006489, 20006507, 20006509, 20006531,
       20006533, 20006543, 20006551, 20006561, 20006579, 20006611,
       20006621, 20006641, 20006663, 20006681, 20006683, 20006717,
       20006731, 20006737, 20006741, 20006743, 20006797, 20006801,
       20006813, 20006827, 20006837, 20006843, 20006851, 20006869,
       20006881, 20006887, 20006923, 20006927, 20006953, 20006957,
       20006971, 20006983, 20006993, 20007017, 20007041, 20007049,
       20007061, 20007107, 20007149, 20007157, 20007167, 20007173,
       20007203, 20007223, 20007257, 20007259, 20007287, 20007293,
       20007301, 20007311, 20007319, 20007359, 20007371, 20007373,
       20007409, 20007457, 20007479, 20007503, 20007523, 20007527,
       20007529, 20007541, 20007557, 20007569, 20007601, 20007643,
       20007661, 20007671, 20007683, 20007719, 20007721, 20007737,
       20007749, 20007773, 20007781, 20007791, 20007811, 20007847,
       20007851, 20007859, 20007863, 20007881, 20007919, 20007971,
       20007973, 20007979, 20008019, 20008039, 20008049, 20008073,
       20008097, 20008127, 20008129, 20008141, 20008151, 20008169,
       20008171, 20008187, 20008199, 20008207, 20008211, 20008217,
       20008223, 20008243, 20008249, 20008253, 20008259, 20008271,
       20008279, 20008291, 20008301, 20008319, 20008327, 20008333,
       20008343, 20008381, 20008447, 20008477, 20008487, 20008523,
       20008529, 20008531, 20008553, 20008559, 20008589, 20008603,
       20008607, 20008621, 20008649, 20008657, 20008663, 20008669,
       20008721, 20008759, 20008801, 20008817, 20008829, 20008871,
       20008903, 20008907, 20008909, 20008913, 20008921, 20008939,
       20008951, 20008991, 20008999, 20009029, 20009047, 20009053,
       20009113, 20009117, 20009137, 20009173, 20009183, 20009243,
       20009273, 20009279, 20009293, 20009329, 20009347, 20009371,
       20009377, 20009383, 20009389, 20009393, 20009411, 20009419,
       20009441, 20009443, 20009447, 20009453, 20009497, 20009513,
       20009531, 20009533, 20009537, 20009543, 20009569, 20009581,
       20009593, 20009597, 20009611, 20009651, 20009653, 20009659,
       20009677, 20009713, 20009719, 20009741, 20009753, 20009761,
       20009813, 20009819, 20009863, 20009887, 20009917, 20009923,
       20009929, 20009939, 20009947, 20009981, 20009987, 20009999],
      dtype=int64)
In [ ]: